package com.sztx.pay.center.core.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.Async;

import com.sztx.pay.center.core.service.AccountFlowService;
import com.sztx.pay.center.core.service.BillnoService;
import com.sztx.pay.center.core.service.bean.UserActFlow;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.se.core.service.impl.BaseServiceImpl;
//@Service

/**
 * 已废弃
 * @author xiaosc
 *
 */
public class AccountFlowServiceImpl extends BaseServiceImpl implements AccountFlowService {
	
	@Autowired
	private BillnoService billnoService;
	
	@Autowired
	private UserActMysqlDAO userActMysqlDAO;
	
	@Autowired
	private UserActFlowMysqlDAO userActFlowMysqlDAO;
	
	@Autowired
	@Qualifier("transactionManager")
	private DataSourceTransactionManager txManager;
	
	@Override
	@Async
	public void recordAccountFlow(UserActFlow actFlow) {
		recordActFlow(actFlow);
	}
	
	
	public void recordActFlow(UserActFlow actFlow){
//		
//		
//		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//
//		TransactionStatus status = txManager.getTransaction(def);
//		try {
//		 
//			LOG.info("Sart Record:"+System.currentTimeMillis());
//			
//			
//			String actNo = actFlow.getActNo();
//			String userId = actFlow.getUserId();
//			int tranAmount = actFlow.getTranAmount();
//			String innerRemark = actFlow.getInnerRemark(); 
//			String userRemark = actFlow.getUserRemark();
//			Date tradeTime = actFlow.getTradeTime();
//			ActFlowTradeType tradeType = actFlow.getTradeType();
//			String tradeOrderId = actFlow.getTradeOrderId();
//			ActType actType = actFlow.getActType();
//			
//			ValidateUtil.isNotBlank(actNo, "用户账户不能为空");
//			ValidateUtil.isNotBlank(userId, "用户账号不能为空");
//			
//			if (tradeType == null) {			
//				throw new BusinessException("交易类别不能为空");
//			}
//				
//			UserActDO userAct = userActMysqlDAO.findByActNoLock(actNo);
//			if (userAct == null) {
//				throw new BusinessException("账号["+actNo+"]不存在");
//			}
//			
//			if (actType == null) {
//				throw new BusinessException("用户账户类别["+actType+"]不能为空");
//			}
//			
//			if (!userId.equals(userAct.getUserId())) {
//				throw new BusinessException("用户["+userId+"]不存 在该账号["+actNo+"]");
//			}
//			
//			int balance = userAct.getActBalance();
//		
//			int modifyCount = userAct.getModifyCount();
//			int actBalance = balance + tranAmount;
//			
//			UserActFlowDO actFlowDO = new UserActFlowDO();
//			
//			String flowNo = billnoService.nextUserAccountFlow();
//			
//			modifyCount = modifyCount + 1; //余额变动次数增加一次	
//			Date cDate = new Date();
//			
//			
//			
//			userActFlowMysqlDAO.saveActFlow(actFlowDO);
//			
//			
//			userActMysqlDAO.updateActBalance(actNo, tranAmount);
//			
//			LOG.info("End Record:"+System.currentTimeMillis());
//			
//		}
//		catch (Exception ex) {
//		  txManager.rollback(status);
//		  throw ex;
//		}
//		txManager.commit(status);
//
////		UserActDO userActDO = new UserActDO();
////		userActDO.setBalanceTime(cDate);
////		userActDO.setModifyCount(modifyCount);
////		userActDO.setActBalance(actBalance);
////		userActDO.setModifyCount(modifyCount);
////		userActMysqlDAO.updateAct(userActDO , actNo);		
	}
	
	
	
	
	
	

	@Override
	@Async
	public void batchRecordAccountFlow(List<UserActFlow> actFlowList) {	
		if (actFlowList != null && !actFlowList.isEmpty()) {
			for (UserActFlow userActFlow : actFlowList) {
				recordAccountFlow(userActFlow);
			}
		}
	}

	@Override
	public void queryAccountFlow(String userId) {
		
	}

}
